//
//  ViewController.m
//  autolayoutAnimationDemo
//
//  Created by tanchao on 15/12/7.
//  Copyright © 2015年 谈超. All rights reserved.
//

#import "ViewController.h"
#import "View+MASAdditions.h"
#import "AnimationUtil.h"
@interface ViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UILabel *topTitleLabel;
@property (nonatomic, strong) UILabel *bottomTitleLabel;
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UITextField *photoTxt;
@property (nonatomic, strong) UIImageView *login_textfield_bottomline;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UILabel *topTipsLabel;
@property (nonatomic, strong) UIView *bottomTipsView;
@property (nonatomic, strong) NSLayoutConstraint *constraint;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareUI];
    self.topTitleLabel.transform = CGAffineTransformMakeTranslation(0, -200);
    self.bottomTitleLabel.transform = CGAffineTransformMakeTranslation(0, -200);
    self.constraint.constant = 0;
    self.iconView.transform = CGAffineTransformMakeTranslation(-200, 0);
}
- (void)viewDidAppear:(BOOL)animated{
   
    [AnimationUtil titleLabelAnimation:self.topTitleLabel withView:self.view];
    [AnimationUtil titleLabelAnimation:self.bottomTitleLabel withView:self.view];
    [AnimationUtil bottomPhoneLineAnimation:self.constraint withView:self.view];
    [AnimationUtil iconViewAnimation:self.iconView withView:self.view];
    [AnimationUtil tipsLabelMaskAnimation:self.topTipsLabel withBeginTime:0];
    [AnimationUtil tipsLabelMaskAnimation:self.bottomTipsView withBeginTime:1];
    [AnimationUtil loginBtnMaskAnimation:self.loginBtn andProgress:0];
     [super viewDidAppear:animated];
}
- (void)prepareUI{
    [self.view addSubview:self.topTitleLabel];
    [self.topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(85);
    }];
    [self.view addSubview:self.bottomTitleLabel];
    [self.bottomTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.topTitleLabel.mas_bottom).offset(5);
    }];
  
    [self.view addSubview:self.photoTxt];
    [self.photoTxt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(290);
        make.top.mas_equalTo(self.bottomTitleLabel.mas_bottom).offset(100);
    }];
    [self.view addSubview:self.iconView];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(17);
        make.height.mas_equalTo(31);
        make.centerY.mas_equalTo(self.photoTxt);
        make.right.mas_equalTo(self.photoTxt.mas_left).offset(-13);
    }];
    [self.view addSubview:self.login_textfield_bottomline];
    [self.login_textfield_bottomline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(345);
        make.height.mas_equalTo(1);
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.photoTxt.mas_bottom).offset(16);
    }];
    [self.view addSubview:self.loginBtn];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(145);
        make.height.mas_equalTo(44);
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.login_textfield_bottomline.mas_bottom).offset(32);
    }];
    [self.view addSubview:self.topTipsLabel];
    [self.topTipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.loginBtn.mas_bottom).offset(14);
    }];
    [self.view addSubview:self.bottomTipsView];
    [self.bottomTipsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.width.mas_equalTo(self.topTipsLabel);
        make.top.mas_equalTo(self.topTipsLabel.mas_bottom).offset(8);
    }];
      self.constraint = self.login_textfield_bottomline.constraints.firstObject;
}
#pragma mark - 限定手机号长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self loginBtnAnimation];
    });
    if (string.length == 0) return YES;
    NSInteger existedLength = textField.text.length;
    NSInteger selectedLength = range.length;
    NSInteger replaceLength = string.length;
  
    return (existedLength - selectedLength + replaceLength <= 11);
}
- (void)loginBtnAnimation{
    [AnimationUtil loginBtnMaskAnimation:self.loginBtn andProgress:(CGFloat)self.photoTxt.text.length/11];
}
#pragma mark - 懒加载控件
- (UILabel *)topTitleLabel{
    if (_topTitleLabel == nil) {
        _topTitleLabel = [[UILabel alloc]init];
        _topTitleLabel.font = [UIFont fontWithName:@"System Light" size:22.0];
        _topTitleLabel.text = @"嘿，Coder";
        [_topTitleLabel sizeToFit];
    }
    return _topTitleLabel;
}
- (UILabel *)bottomTitleLabel{
    if (_bottomTitleLabel == nil) {
        _bottomTitleLabel = [[UILabel alloc]init];
        [_bottomTitleLabel sizeToFit];
        _bottomTitleLabel.font = [UIFont fontWithName:@"System Light" size:22.0];
        _bottomTitleLabel.text = @"请输入手机号";
    }
    return _bottomTitleLabel;
}
- (UIImageView *)iconView{
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc]init];
        _iconView.image = [UIImage imageNamed:@"mobile_71.441860465116px_1159752_easyicon.net"];
    }
    return _iconView;
}
- (UITextField *)photoTxt{
    if (_photoTxt == nil) {
        _photoTxt = [[UITextField alloc]init];
        _photoTxt.keyboardType = UIKeyboardTypePhonePad;
        _photoTxt.delegate = self;
    }
    return _photoTxt;
}
- (UIImageView *)login_textfield_bottomline{
    if (_login_textfield_bottomline == nil) {
        _login_textfield_bottomline = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_textfield_bottomline"]];
    }
    return _login_textfield_bottomline;
}
- (UIButton *)loginBtn{
    if (_loginBtn == nil) {
        _loginBtn = [[UIButton alloc]init];
        _loginBtn.layer.masksToBounds = YES;
        _loginBtn.layer.cornerRadius = 5;
        _loginBtn.backgroundColor = [UIColor redColor];
        [_loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}
- (void)loginBtnClick{
    NSLog(@"手机号是：%@",self.photoTxt.text);
    [self.view endEditing:YES];
}
- (UILabel *)topTipsLabel{
    if (_topTipsLabel == nil) {
        _topTipsLabel = [[UILabel alloc]init];
        _topTipsLabel.font = [UIFont fontWithName:@"System" size:12];
        _topTipsLabel.text = @"登陆/注册请仔细阅读";
        [_topTipsLabel sizeToFit];
    }
    return _topTipsLabel;
}
- (UIView *)bottomTipsView{
    if (_bottomTipsView == nil) {
        _bottomTipsView = [[UIView alloc]init];
        UIButton *leftbtn = [[UIButton alloc]init];
        [leftbtn setTitle:@"服务条款" forState:UIControlStateNormal];
       [leftbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        UILabel *lbl = [[UILabel alloc]init];
        leftbtn.titleLabel.font = [UIFont fontWithName:@"System" size:12];
        lbl.text = @"和";
        lbl.font = [UIFont fontWithName:@"System" size:12];
        [lbl sizeToFit];
        UIButton *rightbtn = [[UIButton alloc]init];
        [rightbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        rightbtn.titleLabel.font = [UIFont fontWithName:@"System" size:12];
        [rightbtn setTitle:@"许可协议" forState:UIControlStateNormal];
        [_bottomTipsView addSubview:lbl];
        [lbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.height.mas_equalTo(_bottomTipsView);
        }];
        [_bottomTipsView addSubview:leftbtn];
        [leftbtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.centerY.height.mas_equalTo(_bottomTipsView);
            make.right.mas_equalTo(lbl.mas_left);
        }];
        [_bottomTipsView addSubview:rightbtn];
        [rightbtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.centerY.height.mas_equalTo(_bottomTipsView);
            make.left.mas_equalTo(lbl.mas_right);
        }];
        
    }
    return _bottomTipsView;
}

@end
